﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

using ECCIT.Extension.Weibo_v2.Common;
using ECCIT.Security;
using ECCIT;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Utility;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.User
{
    public class Login : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string userName = context.Request.Params["username"];
                string password = context.Request.Params["password"];
                string token = context.Request["token"] ?? string.Empty;

                if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("username");
                if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

                ResultInfo res = rexlogin(userName, password, 2052, token);

                if (res.Result)
                {
                    MemberModel mm = res.ReturnObject as MemberModel;
                    UserInfo userInfo = new UserInfo();
                    userInfo.UserID = mm.ID.Value;
                    userInfo.UserName = mm.Name;
                    userInfo.CityID = "";
                    userInfo.CityName = "";
                    if (!string.IsNullOrEmpty(mm.City) && mm.City != "0")
                    {
                        userInfo.CityID = mm.City;
                        userInfo.CityName = DictionaryUtil.GetCityByID(int.Parse(mm.City)).Cnname;
                    }

                    string imgPath = ConfigCacheManager.DomainName;
                    userInfo.Avatar = (string.IsNullOrEmpty(mm.MemberImg)) ?
                        imgPath + ConfigCacheManager.FilePath.DefaultImagePath.VirtualPath + "/" + ConfigCacheManager.FileName.DefaultUserHeadImage :
                        imgPath + ConfigCacheManager.FilePath.GetFrontImageVirtualPath(mm.MemberImg);

                    string s = JsonSerializationHelper.ObjectToJson(userInfo);
                    context.Response.Write(ResultUtility.ReturnResultJson("0", s, "用户登录成功"));
                }
                else
                {
                    context.Response.Write(ResultUtility.ReturnResultJson("-100", "", "用户登录失败. Message：" + res.Message));
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-101", "", "用户登录失败. Message:" + ex.Message));
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }

        private ResultInfo rexlogin(string email, string pass,int langid,string token)
        {
            MemberBLL mbll = new MemberBLL(langid);
            //ResultInfo res = mbll.SignIn(email, pass);
            //增加了token 事件
            ResultInfo res = mbll.SignIn(email, pass, token);
            return res;
        }
    }
}
